package com.mk.web.io.file.client.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Created by WangChen on 2018/12/17 14:17.
 */
public class NioClient {

    InetSocketAddress address = null;
    SocketChannel socketChannel = null;
    // 定义缓存。
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    public void send(){
        address = new InetSocketAddress("localhost", 9999);
        try {
            socketChannel = SocketChannel.open(address);

            String line = "some string 汉字";
            // 将控制台输入的数据写入到缓存。
            buffer.put(line.getBytes("UTF-8"));
            // 重置缓存游标
            buffer.flip();
            // 将数据发送给服务器
            socketChannel.write(buffer);
            // 清空缓存数据。
            buffer.clear();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }
    }
}
